//
//  StrategyNearbySecondTableViewCell.m
//  ITrip
//
//  Created by 1564335 on 15-5-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategyNearbySecondTableViewCell.h"
#import "ParserNearbyModel.h"
#import <UIImageView+AFNetworking.h>
#import <SDImageCache.h>
@interface StrategyNearbySecondTableViewCell()

@property(nonatomic, assign) CGFloat number;

@property(nonatomic, assign) CGFloat photoWidth;

@property(nonatomic, assign) ParNearbyAttraction_contents *contents;


@end

@implementation StrategyNearbySecondTableViewCell

- (void)dealloc
{
    [_descripLabel release];
    [_photo_url release];
    [_photo release];
    [_parserNearbyAC release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.descripLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.descripLabel];
        self.photo_url = [[UIScrollView alloc] init];
        [self.contentView addSubview:self.photo_url];
        [self.descripLabel release];
        [self.photo_url release];
        self.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    }
    return self;
}


- (void)setParserNearbyAC:(ParNearbyAttraction_contents *)parserNearbyAC
{
    
    self.number = 0;
    for (int i = 0; i < parserNearbyAC.notes.count; i++) {
        ParserNearbyNotes *notes = parserNearbyAC.notes[i];
        self.number += 150 * (notes.width / (CGFloat)notes.height);
    }
    
    if (parserNearbyAC.descrip != nil && ![parserNearbyAC isEqual:[NSNull null]]) {
        self.contents = parserNearbyAC;
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
        CGRect frame = [parserNearbyAC.descrip boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width - 40, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        self.descripLabel.frame = CGRectMake(20, 20, self.contentView.frame.size.width  - 40, 30);
        self.descripLabel.text = parserNearbyAC.descrip;
        self.descripLabel.font = [UIFont systemFontOfSize:15];
        self.descripLabel.numberOfLines = 1000;
        [self.descripLabel sizeToFit];
        
        self.photo_url.frame = CGRectMake(20, self.descripLabel.frame.origin.y + frame.size.height + 20, self.contentView.frame.size.width - 40, 150);
        self.photo_url.contentSize = CGSizeMake(self.number, 150);
    } else {
        self.photo_url.frame = CGRectMake(20, 20, self.contentView.frame.size.width - 40, 150);
        self.photo_url.contentSize = CGSizeMake(self.number, 150);
    }
    
    if (self.number > 0) {
        self.photoWidth = 0;
        for (int i = 0; i < parserNearbyAC.notes.count; i++) {
            ParserNearbyNotes *notes = parserNearbyAC.notes[i];
            if (notes.width != 0 && notes.height != 0 && notes.photo_url != nil && ![notes.photo_url isEqual:[NSNull null]]) {
                self.photoWidth += 150 * (notes.width / (CGFloat)notes.height);
                UIImageView *photo = [[UIImageView alloc] init];
                photo.frame = CGRectMake(self.photoWidth + 2 - 150 * (notes.width / (CGFloat)notes.height), 2, 146 * (notes.width / (CGFloat)notes.height), 146);
                [self.photo_url addSubview:photo];
                photo.tag = 1;
                [photo release];
                [photo setImageWithURL:[NSURL URLWithString:notes.photo_url] placeholderImage:[UIImage imageNamed:@"defaultPicture"]];
//                NSLog(@"%f", self.photoWidth);
            }
            
        }
    }
//    NSLog(@"%@", self.descripLabel.text);
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
